//
//  CollectPhotoVC.m
//  HomeFurnishing
//
//  Created by dllo on 16/4/9.
//  Copyright © 2016年 YMing. All rights reserved.
//




#import "CollectPhotoVC.h"
#import "MyLayout.h"
#import "GalCollectionViewCell.h"
#import "GalBaseModel.h"
#import "ChannelView.h"
#import "DetailGalVC.h"
@interface CollectPhotoVC ()<UICollectionViewDelegate, UICollectionViewDataSource, MyLayoutDelegate>
@property(nonatomic, retain)UICollectionView *collectView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation CollectPhotoVC

-(void)dealloc
{
    [_collectView release];
    [_dataArr release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self creatrfromSQL];
    [self.collectView reloadData];
}
-(void)creatrfromSQL
{
    self.dataArr = [[DataBaseManager shareInstance ] selectAllphotos];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:(UIBarButtonItemStylePlain) target:self action:@selector(removeData)];
    [self createCollectionView];
}
/**
 *  collectionView
 */

-(void)createCollectionView
{
    MyLayout *layout = [[MyLayout alloc] init];
    //列间距
    layout.columnSpace = 10;
    //行间距
    layout.rowSpace = 10;
    //四边距
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    //列数
    layout.columnCounts = 2;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) collectionViewLayout:layout];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectView];
    [_collectView registerClass:[GalCollectionViewCell class] forCellWithReuseIdentifier:@"GalCVcell"];
    layout.delegate =self;
    
}
/**
 *  实现自定义layout协议方法
 *
 *  @param collectionView
 *  @param indexPath      位置信息
 *  @param width          宽度
 *
 *  @return 高度
 */
-(CGFloat)collectionView:(UICollectionView *)collectionView heightForItemAtIndexPath:(NSIndexPath *)indexPath byWidth:(CGFloat)width
{
    //获取数据
    GalBaseModel *model = _dataArr[indexPath.item];
    CGFloat h = model.height.doubleValue/model.width.doubleValue *width;
    return h;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GalCVcell" forIndexPath:indexPath];
    GalBaseModel *model = _dataArr[indexPath.item];
    NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w%@_h%@_m0.jpg",model.mId,model.width,model.height];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    return cell;
}

-(void)removeData
{
    [[DataBaseManager shareInstance] dropTablePhoto];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
